﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Common;
using MyWeb.Business;
using MyWeb.Data;
namespace MyWeb.Admins
{
    public partial class tblNews : System.Web.UI.Page
    
    {
        static bool Insert = false;protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack){Session["folder"] = "tblNews"; BindGrid();
            }
        }
private void BindGrid()
{
    grdtblNews.DataSource = tblNewsService.tblNews_GetByAll() ;
    grdtblNews.DataBind();
}
protected void btnThem_Click(object sender, EventArgs e)
{pnShow.Visible = false;
    pnUpdate.Visible = true;
    ControlClass.ResetControlValues(this);
    Session["upload"] = null;Insert=true;}
protected void btnXoa_Click(object sender, EventArgs e)
{
    try
    {
        DataGridItem item = default(DataGridItem);
        for (int i = 0; i < grdtblNews.Items.Count; i++)
        {
            item = grdtblNews.Items[i];
            if (item.ItemType == ListItemType.AlternatingItem | item.ItemType == ListItemType.Item)
            {
                if (((CheckBox)item.FindControl("ChkSelect")).Checked)
                {
                    string strId = item.Cells[1].Text;
                    try
                    {
                        tblNewsService.tblNews_Delete(strId);
                    }
                    catch
                    {
                    }
                }
            }
        }
        grdtblNews.CurrentPageIndex = 0;BindGrid();
    }
    catch
    {
    }
}
        protected void grdtblNews_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            Data.tblNewsInfo strCA=new tblNewsInfo();
strCA._nid = e.CommandArgument.ToString();
if  (e.CommandName=="Edit"){DataTable dt = tblNewsService.tblNews_GetById(strCA);txt_nid.Text=dt.Rows[0]["_nid"].ToString();txt_ntitle.Text=dt.Rows[0]["_ntitle"].ToString();txt_ncontent.Text=dt.Rows[0]["_ncontent"].ToString();txt_ncreatedate.Text=dt.Rows[0]["_ncreatedate"].ToString();pnUpdate.Visible = true;pnShow.Visible = false;}else{tblNewsService.tblNews_Delete(strCA._nid);BindGrid();
}
        }
        protected void lblBack_Click(object sender, EventArgs e){pnUpdate.Visible = false;pnShow.Visible = true;Insert=false;}protected void lblUpdate_Click(object sender, EventArgs e){string image = Session["upload"] == null ? "" : Session["upload"].ToString();
    #region[TestInput]
        if (txt_nid.Text.Trim().Equals("")) { WebMsgBox.Show("_nid can not empty !"); txt_nid.Focus(); return; } if (txt_ntitle.Text.Trim().Equals("")) { WebMsgBox.Show("_ntitle can not empty !"); txt_ntitle.Focus(); return; } if (txt_ncontent.Text.Trim().Equals("")) { WebMsgBox.Show("_ncontent can not empty !"); txt_ncontent.Focus(); return; } if (txt_ncreatedate.Text.Trim().Equals("")) { WebMsgBox.Show("_ncreatedate can not empty !"); txt_ncreatedate.Focus(); return; }
#endregion
Data.tblNewsInfo obj=new Data.tblNewsInfo();obj._nid = txt_nid.Text;obj._ntitle = txt_ntitle.Text;obj._ncontent = txt_ncontent.Text;obj._ncreatedate = txt_ncreatedate.Text;if (Insert == true){tblNewsService.tblNews_Insert(obj);}else{tblNewsService.tblNews_Update(obj);}BindGrid();pnShow.Visible = true;pnUpdate.Visible = false;Insert = false;
}protected void grdtblNews_PageIndexChanged(object source, DataGridPageChangedEventArgs e){grdtblNews.CurrentPageIndex = e.NewPageIndex;BindGrid();} protected void grdtblNews_ItemDataBound(object sender, DataGridItemEventArgs e)
{
ListItemType itemType = e.Item.ItemType;if ((itemType != ListItemType.Footer) && (itemType != ListItemType.Separator)){if (itemType == ListItemType.Header){object checkBox = e.Item.FindControl("chkSelectAll");if ((checkBox != null)){((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelectAll_OnClick(this)");
}
}else{
string tableRowId = grdtblNews.ClientID + "_row" + e.Item.ItemIndex.ToString();
e.Item.Attributes.Add("id", tableRowId);
object checkBox = e.Item.FindControl("chkSelect");
if ((checkBox != null))
{e.Item.Attributes.Add("onMouseMove", "Javascript:chkSelect_OnMouseMove(this)");
e.Item.Attributes.Add("onMouseOut", "Javascript:chkSelect_OnMouseOut(this," + e.Item.ItemIndex + ")");((CheckBox)checkBox).Attributes.Add("onClick", "Javascript:chkSelect_OnClick(this," + e.Item.ItemIndex + ")");}}}}
}
}